package com.foxzzz.binary.search;

public class Solution74 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int low = 0;

        boolean result = false;

        if (matrix.length > 0 && matrix[0].length > 0) {
            int sum = matrix.length * matrix[0].length;
            int high = sum;
            while (low <= high) {
                int mid = (low + high) >> 1;
                if (mid >= sum)
                    break;
                int x = mid % matrix[0].length;
                int y = mid / matrix[0].length;

                if (target == matrix[y][x]) {
                    result = true;
                    break;
                }
                if (target > matrix[y][x])
                    low = mid + 1;
                else
                    high = mid - 1;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] nums = new int[1][1];
        nums[0][0] = 1;
        System.out.println(new Solution74().searchMatrix(nums, 2));
    }
}
